package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OederSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author ：hg
 * @date ：Created in 2021-12-10 22:58
 * @description：预约设置服务
 */
@Component
@Service(interfaceClass = OederSettingService.class)
public class OederSettingServiceImpl implements OederSettingService {

    //注入
    @Autowired
    private OrderSettingDao oederSettingDao;

    /**
     * 批量导入预约设置数据
     *
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = oederSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    //已经进行了预约设置，执行更新
                    oederSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置，执行插入操作
                    oederSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     *
     * @param date 传过来的日期 yyyy-mm
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        //调用dao,根据日期查询数据库的结果集
        List<OrderSetting> list = oederSettingDao.getOrderSettingByMonth(map);
        //创造返回页面的结果集
        List<Map> result = new ArrayList<>();
        //遍历数据库结果集，进行构造返回页面的结果集
        if (list != null & list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate());//存放日期数字就是多少号
                m.put("number", orderSetting.getNumber());//可预约人数
                m.put("reservations", orderSetting.getReservations());//已预约人数
                result.add(m);
            }
        }
        return result;
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        //根据日期进行查询是否已经进行了预约设置
        Date orderDate = orderSetting.getOrderDate();
        long count = oederSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
            //当前日期进行了预约设置，需要执行更新操作
            oederSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，需要执行插入操作
            oederSettingDao.add(orderSetting);
        }


    }
}


